<template>
  <el-dialog width="600px" :title="dialogTitle" :visible.sync="dialogVisible" @close="handleClose">
    <el-form
      ref="submitForm"
      v-loading="formLoading"
      :model="submitForm"
      :rules="submitFormRules"
      label-width="100px"
      label-position="right"
      class="submit-form"
    >
      <el-form-item label="序列编码" prop="sequenceCode">
        <el-input v-model="submitForm.sequenceCode" placeholder="请输入序列代码" />
      </el-form-item>
      <el-form-item label="序列名称" prop="sequenceName">
        <el-input v-model="submitForm.sequenceName" placeholder="请输入序列名称" />
      </el-form-item>
      <el-form-item label="最大长度" prop="length">
        <el-input v-model="submitForm.length" type="number" placeholder="请输入最大序列长度" />
      </el-form-item>
      <el-form-item label="前缀规则" prop="prefixRule">
        <dict-select v-model="submitForm.prefixRule" dict-key="seq_prefix_rule" placeholder="请选择前缀规则" />
      </el-form-item>
      <el-form-item v-if="submitForm.prefixRule==='1'" label="前缀" prop="prefix">
        <el-input v-model="submitForm.prefix" placeholder="请输入前缀" />
      </el-form-item>
      <el-form-item label="是否补齐" prop="autoFill">
        <dict-select v-model="submitForm.autoFill" dict-key="true_or_false" placeholder="请选择是否补齐" />
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :loading="formLoading" type="text" @click="handleClose">取消</el-button>
      <el-button :loading="formLoading" type="primary" @click="handleSubmit">提交</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { saveSequenceRule, updateSequenceRule, selectSequenceRuleById } from '@/api/system/sequenceRule'
/**
 * @author yizuomin
 * @date 2023/5/7 19:05
 **/
export default {
  name: 'SubmitModule',
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      dialogVisible: this.visible,
      dialogTitle: '',
      formLoading: false,
      submitForm: {
        id: '',
        sequenceName: '',
        sequenceCode: '',
        length: '',
        prefixRule: '',
        prefix: '',
        autoFill: '1'
      },
      submitFormRules: {
        sequenceName: [{ required: true, message: '请输入序列名称', trigger: 'change' }],
        sequenceCode: [{ required: true, message: '请输入序列代码', trigger: 'change' }],
        length: [{ required: true, message: '请输入序列长度', trigger: 'change' }],
        prefixRule: [{ required: true, message: '请选择序列前缀规则', trigger: 'change' }],
        prefix: [{ required: true, message: '请输入序列前缀', trigger: 'change' }],
        autoFill: [{ required: true, message: '请选择是否补齐', trigger: 'change' }]
      }
    }
  },
  methods: {
    init(id) {
      if (!id) {
        this.dialogTitle = '新增序列规则'
        return
      }
      this.dialogTitle = '修改序列规则'
      this.formLoading = true
      selectSequenceRuleById(id).then(resp => {
        if (resp.success) {
          this.submitForm = resp.data
        }
      }).finally(() => {
        this.formLoading = false
      })
    },
    handleSubmit() {
      this.$refs.submitForm.validate((valid) => {
        if (valid) {
          let request = saveSequenceRule
          if (this.submitForm.id) {
            request = updateSequenceRule
          }
          this.formLoading = true
          request(this.submitForm).then(resp => {
            if (resp.success) {
              this.$message.success('操作成功')
              this.$refs.submitForm.resetFields()
              this.handleClose()
            }
          }).finally(() => {
            this.formLoading = false
          })
        }
      })
    },
    handleClose() {
      this.$emit('close')
    }
  }
}
</script>

<style scoped>

</style>
